package com.xyzq.cgate.admin.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.base.Preconditions;
import com.xyzq.cgate.admin.service.SignService;
import com.xyzq.cgate.admin.wrapper.SignWrapper;
import com.xyzq.cgate.common.constant.ZkPathConstants;
import com.xyzq.cgate.common.entity.Plugin;
import com.xyzq.cgate.common.entity.Sign;
import com.xyzq.cgate.common.response.ResponseEntity;
import com.xyzq.cgate.common.utils.IdUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.stereotype.Controller;
import reactor.core.publisher.Mono;

import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenchen
 * @since 2019-06-25
 */
@RestController
@RequestMapping("/admin/sign")
public class SignController {

    @Autowired
    SignService signService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public Mono<ResponseEntity> list() {
        List<Sign> list = signService.selectList(new EntityWrapper());
        list.stream().map(p -> SignWrapper.wrapper(p))
                .collect(Collectors.toList());
        return Mono.just(new ResponseEntity<>().success(list));
    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public Mono<ResponseEntity> create(@RequestBody Sign sign) {
        Preconditions.checkNotNull(sign, "sign为null");
        Preconditions.checkArgument(StringUtils.isNoneBlank(sign.getAppKey()));
        Preconditions.checkArgument(StringUtils.isNoneBlank(sign.getAppSecret()));
        Preconditions.checkArgument(StringUtils.contains("01", sign.getEnabled().toString()));
        sign.setId(IdUtil.generateUUID());
        signService.insert(sign);
        sync();
        return Mono.just(new ResponseEntity<>().success());
    }

    @RequestMapping(value = "sync", method = RequestMethod.GET)
    public Mono<ResponseEntity> sync() {
        List<Sign> list = signService.selectList(new EntityWrapper());
        signService.removeAllZkDataByParentPath(ZkPathConstants.SIGN_PARENT_PATH);
        signService.insertSignDataToZk(list);
        return Mono.just(new ResponseEntity().success());
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Mono<ResponseEntity> update(@RequestBody Sign sign) {
        Preconditions.checkNotNull(sign, "sign为null");
        Preconditions.checkNotNull(sign.getId(), "id不能为空");
        Preconditions.checkArgument(StringUtils.isNoneBlank(sign.getAppKey()));
        Preconditions.checkArgument(StringUtils.isNoneBlank(sign.getAppSecret()));
        Preconditions.checkArgument(StringUtils.contains("01", sign.getEnabled().toString()));
        EntityWrapper<Sign> entityWrapper = new EntityWrapper();
        entityWrapper.eq("id", sign.getId());
        signService.update(sign, entityWrapper);
        sync();
        return Mono.just(new ResponseEntity<>().success());
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public Mono<ResponseEntity> delete(@RequestBody Sign sign) {
        Preconditions.checkNotNull(sign, "sign为null");
        Preconditions.checkNotNull(sign.getId(), "id不能为空");
        EntityWrapper<Sign> entityWrapper = new EntityWrapper();
        entityWrapper.eq("id", sign.getId());
        signService.delete(entityWrapper);
        sync();
        return Mono.just(new ResponseEntity<>().success());
    }

    @RequestMapping(value = "switchOpen", method = RequestMethod.POST)
    public Mono<ResponseEntity> switchOpen(@RequestBody Sign sign) {
        Preconditions.checkNotNull(sign, "sign为null");
        Preconditions.checkNotNull(sign.getId(), "id不能为空");
        EntityWrapper<Sign> entityWrapper = new EntityWrapper();
        entityWrapper.eq("id", sign.getId());
        sign.setEnabled(1 - sign.getEnabled());
        signService.update(sign, entityWrapper);
        sync();
        return Mono.just(new ResponseEntity<>().success());
    }


}

